﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BLL;
using WEB.Models;

namespace WEB.Controllers
{
    public class AutenticarController : Controller
    {
        //
        // GET: /Autenticar/

        public ActionResult Index()
        {
            //Usuario usuar = new Usuario();
            //ViewData["DadosUsuarios"] = usuar.Listar();
            if (Request.IsAuthenticated) {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Autenticar/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Autenticar/Create

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Autenticar(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Usuario us = new Usuario();
                    tbUsuario usuario = us.ValidarUsuario(collection[0]);
                    if (usuario != null)
                    {
                        if (usuario.txtSenha.CompareTo(collection[1]) == 0)
                        {
                        FormsAuthentication.SetAuthCookie(usuario.idUsuario.ToString(), false);
                        return RedirectToAction("Index", "Home");
                        }
                       
                        //userManager.Add(user);
                       
                    }
                    else
                    {
                        ModelState.AddModelError("", "Usuário inválido");
                    }
                }
            }
            catch
            {
                return RedirectToAction("Index", "Home");
            }

            //return View();
            return RedirectToAction("Index", "Home");
        }

        //
        // POST: /Autenticar/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Autenticar/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Autenticar/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Autenticar/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Autenticar/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Sair()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }
    }
}
